/**
 * @file dbdate.c
 * @brief Fichier source du module dbdate.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

                       Librairies de database

********************************************************************* */

#include "dbdate.h"

/* *********************************************************************

                        Fonctions

********************************************************************* */
void recuperation_DDat(Date **date, const int id)
{
  if(date)
    {
      if(id > 0)
	{
	  DataBase data;
	  ouvrir_Db();
	  /* Récupération de ceux qui ont comme père id */
	  prepare_Db(&data, "select rowid, jour, mois, annee from date where rowid = ?");
	  bindValueInt_Db(&data, 1, id);
	  if(execute_Db(&data))
	    {
	      if((*date) == NULL)
		(*date) = creer_Dat();
	      setId_Dat((*date), getValueInt_Db(&data, 0));
	      setJour_Dat((*date), getValueInt_Db(&data, 1));
	      setMois_Dat((*date), getValueInt_Db(&data, 2));
	      setAnnee_Dat((*date), getValueInt_Db(&data, 3));
	    }
	  else
	    {
	      (*date) = NULL;
	    }
	  finalise_Db(&data);
	}
      else
	(*date) = NULL;
    }
}

void insertion_DDat(Date *date)
{
  if(date != NULL)
    {
      DataBase data;
      ouvrir_Db();
      if(getSup_Dat(date) == 1)
	{
	  suppression_DDat(getId_Dat(date));
	  return;
	}
      else
	{
	  if(getId_Dat(date) == 0)
	    {
	      prepare_Db(&data, "insert into date (jour, mois, annee) values (?, ?, ?)");
	    }
	  else
	    {
	      if(getMaj_Dat(date) == 1)
		{
		  prepare_Db(&data, "update date set jour = ?, mois = ?, annee = ? where rowid = ?");
		  bindValueInt_Db(&data, 4, getId_Dat(date));
		}
	    }
	}
      if(getId_Dat(date) == 0 || getMaj_Dat(date) == 1)
	{
	  bindValueInt_Db(&data, 1, getJour_Dat(date));
	  bindValueInt_Db(&data, 2, getMois_Dat(date));
	  bindValueInt_Db(&data, 3, getAnnee_Dat(date));
	  if(execute_Db(&data))
	    {
	      
	    }
	  finalise_Db(&data);
	  if(getId_Dat(date) == 0)
	    {
	      prepare_Db(&data, "select max(rowid) from date");
	      if(execute_Db(&data))
		{
		  setId_Dat(date, getValueInt_Db(&data, 0));
		}
	      finalise_Db(&data);
	    }
	}
    }
}

void suppression_DDat(const int id)
{
  if(id > 0)
    {
      DataBase data;
      ouvrir_Db();
      
      prepare_Db(&data, "delete from date where rowid = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Oeuvre where id_DateSortie = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {
	  
        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Personne where id_DateNaissance = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Personne where id_DateDeces = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
	{
	  
        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Regroupement where id_DateCreation = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Film where id_DateDVD = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {
	  
        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Film where id_DateBlueRay = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
      prepare_Db(&data, "delete from Film where id_DateProduction = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
    }
}

